<?php

namespace plugin\ycMassage\app\api\controller;

use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageUserFinanceAccount;
use plugin\ycMassage\app\model\MassageUserWechat;
use plugin\ycMassage\utils\enum\FinanceAccountChannels;
use support\Request;

class UserFinanceAccountController extends BaseController
{
    public function index(Request $request)
    {
        $UserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $request->uid])->field('channels,name,mobile,qrcode,account,bank')->select();
        $data = [];
        foreach ($UserFinanceAccount as $item) {
            $data[$item->channels] = $item->toArray();
        }
        return parent::successRes($data);
    }
    public function save(Request $request)
    {
        $D = $request->post();
        if (empty($D['channels'])) {
            return parent::fail('参数错误');
        }
        if (empty($D['name']) || empty($D['mobile'])) {
            return parent::fail('姓名和手机号不能为空');
        }
        $UserFinanceAccount = MassageUserFinanceAccount::where(['uid' => $request->uid, 'channels' => $D['channels']])->find();
        if (!$UserFinanceAccount) {
            $UserFinanceAccount = new MassageUserFinanceAccount;
            $UserFinanceAccount->uid = $request->uid;
            $UserFinanceAccount->saas_appid = $request->saas_appid;
            $UserFinanceAccount->channels = $D['channels'];
        }
        $UserFinanceAccount->name = $D['name'];
        $UserFinanceAccount->mobile = $D['mobile'];
        switch ($UserFinanceAccount->channels) {
            case FinanceAccountChannels::ALIPAY_PAYMENT_CODE['value']:
            case FinanceAccountChannels::WECHAT_PAYMENT_CODE['value']:
                if (empty($D['qrcode'])) {
                    return parent::fail('请上传收款码');
                }
                $UserFinanceAccount->qrcode = $D['qrcode'];
                break;
            case FinanceAccountChannels::WECHAT_TRANSFER['value']:
                $MassageUserWechat = MassageUserWechat::where(['uid' => $request->uid])->find();
                if (!$MassageUserWechat) {
                    return parent::fail('请先绑定微信');
                }
                if (empty($MassageUserWechat->openid) && empty($MassageUserWechat->mp_openid)) {
                    return parent::fail('请先绑定微信');
                }
                if ($MassageUserWechat->openid) {
                    $UserFinanceAccount->openid = $MassageUserWechat->openid;
                }
                if ($MassageUserWechat->mp_openid) {
                    $UserFinanceAccount->mp_openid = $MassageUserWechat->mp_openid;
                }
                break;
            case FinanceAccountChannels::BANK_TRANSFER['value']:
                if (empty($D['account']) || empty($D['bank'])) {
                    return parent::fail('银行卡号和开户行不能为空');
                }
                $UserFinanceAccount->account = $D['account'];
                $UserFinanceAccount->bank = $D['bank'];
                break;
        }
        if (!$UserFinanceAccount->save()) {
            return parent::fail('保存失败');
        }
        return parent::success('保存成功');
    }
}
